package com.cmcc.zysoft.infocollection.service;

import java.io.File;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.cmcc.zysoft.infocollection.dao.EmployeeDao;
import com.cmcc.zysoft.infocollection.model.Department;
import com.cmcc.zysoft.infocollection.model.Employee;
import com.cmcc.zysoft.infocollection.model.Headship;
import com.cmcc.zysoft.infocollection.util.ConcatPinyin;
import com.starit.common.dao.hibernate.HibernateBaseDao;
import com.starit.common.dao.service.BaseServiceImpl;
/**
 * @author 喻金龙
 * <br />邮箱: yu.jinlong@ustcinfo.com
 * <br />描述: EmployeeService.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-11 下午2:33:01
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Service
public class EmployeeService extends BaseServiceImpl<Employee, String> {
	
	@Resource
	private EmployeeDao employeePcDao;
	
	@Override
	public HibernateBaseDao<Employee, String> getHibernateBaseDao() {
		return this.employeePcDao;
	}
	
	/**
	 * 属性名称: departmentPcService.
	 * 类       型: DepartmentPcService.
	 */
	@Resource
	private DepartmentPcService departmentPcService;
	
	/**
	 * 属性名称: headshipPcService.
	 * 类       型: HeadshipPcService.
	 */
	@Resource
	private HeadshipPcService headshipPcService;
	
	/**
	 * 获取员工列表.
	 * @param page
	 * @param rows
	 * @param keyword 搜索关键字
	 * @param type 类型
	 * @param compOrDpetId 企业或者ID
	 * @return Map<String, Object>
	 */
	public Map<String, Object> getAllEmployee(int page, int rows, String keyword, String id) {
		if(StringUtils.hasText(id)) {
			String type = "";
			String[] type_id = id.split("_");
			if("c".equals(type_id[0])) {
				type = "company";
			} else {
				type = "department";
			}
			return this.employeePcDao.getAllEmployee(page, rows, keyword, type, type_id[1]);
		} else {
			return this.employeePcDao.getAllEmployee(page, rows, keyword);
		}
	}
	
	/**
	 * 部门类型下拉框
	 * @param 
	 * @return
	 */
	public List<Map<String, Object>> departmentTree() {
		return this.employeePcDao.departmentTree();
	}
	
	/**
	 * 根据企业ID获取其岗位列表.
	 * @param companyId 企业ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> headships(String companyId) {
		return this.employeePcDao.headships(companyId);
	}
	
	/**
	 * 获取角色列表.
	 * @return List<Map<String,Object>>
	 */
	public List<Map<String, Object>> roles() {
		return this.employeePcDao.roles();
	}
	
	/**
	 * 根据员工id获取员工信息
	 * @param employeeId 员工id
	 * @return
	 */
	public Employee getEmployee(String employeeId) {
		return this.employeePcDao.getEmployee(employeeId);
	}
	
	/**
	 * 判断员工编号是否同名
	 * @param employeeCode
	 * @return true表示同名   false表示不同名
	 */
	public boolean isEmployeeCodeEqual(String employeeCode,String employeeId){
		return this.employeePcDao.isEmployeeCodeEqual( employeeCode, employeeId);
	}
	
	/**
	 * 修改员工信息
	 * @param employee 员工对象
	 */
	public void updateEmployee(Employee employee) {
		this.employeePcDao.updateEmployee(employee);
	}
	
	/**
	 * 判断手机号码是否存在
	 * @param 
	 * @return true表示同名   false表示不同名
	 */
	public boolean isPhoneNumexist(String phoneNumber , String employeeId){
		return this.employeePcDao.isPhoneNumexist(phoneNumber ,employeeId);
	}
	
	/**
	 * 根据员工id删除员工信息
	 * @param employeeId 员工id
	 */
	public void deleteEmployee(String employeeId) {
		this.employeePcDao.deleteEmployee(employeeId);
	}
	
	/**
	 * 删除员工时 自动物理删除  用户表里的员工
	 * @param employeeId
	 */
	public void deleteSystemUser(String employeeId) {
		this.employeePcDao.deleteSystemUser(employeeId);
	}
	
	/**
	 * 删除员工时 自动物理删除  用户角色表相关信息
	 * @param UserId
	 */
	public void deleteUserRole(String UserId) {
		this.employeePcDao.deleteUserRole(UserId);
	}
	
	/**
	 * 判断excel导入时数据的有效性.
	 * @param workbook excel源文件
	 * @return String
	 */
	public String checkDataValidity(Workbook workbook) {
		String result = ""; //返回结果
		String companyName = ""; //公司名称
		String departmentName = ""; //部门名称
		String headshipName =  ""; //岗位名称 
		String userName = ""; //用户姓名
		String mobile = ""; //用户手机号码
		Sheet sheet = workbook.getSheet(0);
		int rows = sheet.getRows();
		//循环判断是否有无效数据-只要有一个无效数据,则停止判断
		for(int r = 1; r < rows; r++) {
			companyName = sheet.getCell(1, r).getContents().trim();
			departmentName = sheet.getCell(2, r).getContents().trim();
			headshipName = sheet.getCell(3, r).getContents().trim();
			userName = sheet.getCell(4, r).getContents().trim();
			mobile = sheet.getCell(5, r).getContents().trim();
			if(!StringUtils.hasText(companyName)) {
				result += "第" + (r + 1) + "行公司名称为空，导入失败";
				break;
			}
			if(!StringUtils.hasText(departmentName)) {
				result += "第" + (r + 1) + "行部门名称为空，导入失败";
				break;
			}
			if(!StringUtils.hasText(headshipName)) {
				result += "第" + (r + 1) + "行岗位名称为空，导入失败";
				break;
			}
			if(!StringUtils.hasText(userName)) {
				result += "第" + (r + 1) + "行姓名为空，导入失败";
				break;
			}
			if("".equals(mobile) || mobile.length() < 11 || mobile.length() > 11) {
				result += "第" + (r + 1) + "行电话号码有误，导入失败";
				break;
			} else {
				if(this.employeePcDao.isPhoneNumexist(mobile, "0")) {
					result += "第" + (r + 1) + "行手机号码在系统中已存在，导入失败";
					break;
				}
			}
		}
		return result;
	}
	
	/**
	 * 创建新的excel文件,并填充无效数据的背景色为黄色.
	 * @param workbook excel源文件
	 * @param excelFile excel目标文件
	 * @throws Exception
	 */
	public String generateExcel(Workbook workbook, File excelFile) throws Exception {
		//复制excel文件
		WritableWorkbook writableWorkbook = Workbook.createWorkbook(excelFile, workbook);
		WritableSheet sheet = writableWorkbook.getSheet(0);
		//定义单元格样式
		WritableFont font = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
		WritableCellFormat wcf = new WritableCellFormat(font); //单元格定义
		wcf.setBackground(Colour.YELLOW); //设置背景色为黄色
		wcf.setAlignment(Alignment.CENTRE); //设置居中
		int rows = sheet.getRows(); //取excel行数
		String companyName = ""; //公司名称
		String departmentName = ""; //部门名称
		String headshipName =  ""; //岗位名称
		String userName = ""; //用户姓名
		String mobile = ""; //用户手机号码
		for(int r = 1; r < rows; r++) {
			companyName = sheet.getCell(1, r).getContents().trim();
			departmentName = sheet.getCell(2, r).getContents().trim();
			headshipName = sheet.getCell(3, r).getContents().trim();
			userName = sheet.getCell(4, r).getContents().trim();
			mobile = sheet.getCell(5, r).getContents().trim();
			if(!StringUtils.hasText(companyName)) { //公司名称为空,为单元格添加背景色
				sheet.addCell(generateLabel(1, r, companyName, wcf));
			}
			if(!StringUtils.hasText(departmentName)) { //部门名称为空,为单元格添加背景色
				sheet.addCell(generateLabel(2, r, departmentName, wcf));
			}
			if(!StringUtils.hasText(headshipName)) { //岗位名称为空,为单元格添加背景色
				sheet.addCell(generateLabel(3, r, headshipName, wcf));
			}
			if(!StringUtils.hasText(userName)) { //用户姓名为空,为单元格添加背景色
				sheet.addCell(generateLabel(4, r, userName, wcf));
			}
			if("".equals(mobile) || mobile.length() < 11 || mobile.length() > 11) { //手机号码无效,为单元格添加背景色
				sheet.addCell(generateLabel(5, r, mobile, wcf));
			} else {
				if(this.employeePcDao.isPhoneNumexist(mobile, "0")) { //手机号码重复,为单元格添加背景色
					sheet.addCell(generateLabel(5, r, mobile, wcf));
				}
			}
		}
		writableWorkbook.write();
		writableWorkbook.close();
		return excelFile.getName();
	}
	
	/**
	 * 根据行列数生成单元格.
	 * @param row 行数
	 * @param col 列数
	 * @param content 单元格内容
	 * @param wcf 
	 * @return Label
	 */
	private static Label generateLabel(int row, int col, String content, WritableCellFormat wcf) {
		return new Label(row, col, content, wcf);
	}
	
	/**
	 * 添加员工.
	 * @param departmentId 部门ID
	 * @param headshipId 岗位ID
	 * @param employeeName 员工姓名
	 * @param mobile 手机号码
	 * @return String(员工ID)
	 */
	@Transactional
	public String addEmployee(String departmentId, String headshipId, String employeeName, String mobile) {
		Employee employee = new Employee();
		Department department = this.departmentPcService.getEntity(departmentId);
		Headship headship = this.headshipPcService.getEntity(headshipId);
		employee.setDepartment(department);
		employee.setHeadship(headship);
		employee.setEmployeeCode(ConcatPinyin.changeToPinyin(employeeName));
		employee.setEmployeeName(employeeName);
		employee.setMobile(mobile);
		employee.setDelFlag("0");
		return this.employeePcDao.save(employee);
	}
}